package com.film.controller;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.film.domain.Film;
import com.film.domain.FilmShowings;
import com.film.service.FilmService;
import com.film.service.ShowingsService;
import com.film.service.impl.FilmServiceImpl;
import com.film.service.impl.ShowingsServiceImpl;

@WebServlet("/changeDateShowings")
public class ChangeDateShowingsController extends HttpServlet{
	
	private ShowingsService showingsService = new ShowingsServiceImpl();
	private FilmService filmService = new FilmServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String filmId = request.getParameter("filmId");
		String date = request.getParameter("date");
		String flag = request.getParameter("flag");
		System.out.println(date);
		date = transferDate(date);
		List<String> showingsDateList = showingsService.selectShowingsDateByFilm(Integer.parseInt(filmId), 0);
		if(showingsDateList.size() == 0) {
			Date d = new Date();
			DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String str = format.format(d);
			showingsDateList.add(str);
		}
		System.out.println(date);
		List<FilmShowings>  showingsList = showingsService.selectShowingsByDate(date, Integer.parseInt(filmId), 0);
		for (FilmShowings filmShowings : showingsList) {
			System.out.println(filmShowings);
		}
		//按放映时间排序
		if(showingsList != null && showingsList.size() != 0) {
			showingsList.sort(new Comparator<FilmShowings>() {
				@Override
				public int compare(FilmShowings o1, FilmShowings o2) {
					return o1.getShowingsStartTime().compareTo(o2.getShowingsStartTime());
				}
			});
		}
		Film filmDetails = filmService.FilmOneTypeAndDirectoe(Integer.parseInt(filmId));
		HttpSession session = request.getSession();
		session.setAttribute("showingsDateList", showingsDateList);
		session.setAttribute("filmDetails", filmDetails);
		session.setAttribute("showingsList", showingsList);
		response.sendRedirect("showings.jsp?flag="+flag);
	}
	
	private String transferDate(String date) {
		//6月7日 ---> 2021-6-7
		date = "2021年"+date;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
		String s2 = "";
		try {
			Date date1 = sdf.parse(date);
			s2 = sdf2.format(date1);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return s2;
	}
}
